package com.open.capacity.user.service.impl;

import com.open.capacity.common.model.InterResume;
import com.open.capacity.common.model.SysResume;
import com.open.capacity.common.model.SysRole;
import com.open.capacity.common.model.SysUser;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.user.dao.InterResumeDao;
import com.open.capacity.user.dao.SysUserDao;
import com.open.capacity.user.dao.SysUserRoleDao;
import com.open.capacity.user.dto.ResumeDto;
import com.open.capacity.user.model.FileInfo;
import com.open.capacity.user.service.InterResumeService;
import com.sun.deploy.net.proxy.pac.PACFunctions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
public class InterResumeServiceImpl implements InterResumeService {


    @Autowired
    private InterResumeDao interResumeDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public int save(InterResume interResume) {
        return interResumeDao.save(interResume);
    }

    @Override
    public int delete(Long id) {
        return interResumeDao.delete(id);
    }

    @Override
    public int count(Map<String, Object> params) {
        return interResumeDao.count(params);
    }

    @Override
    public PageResult<InterResume> findList(Map<String, Object> params) {
        Object userId1 = params.get("userId");
        long userId = 0;
        boolean isAdmin=false; //默认不是管理员
        if(userId1!=null){
            userId = Long.valueOf(params.get("userId").toString());
            Set<SysRole> rolesByUserId = sysUserRoleDao.findRolesByUserId(userId);
            Iterator<SysRole> iterator = rolesByUserId.iterator();
            while (iterator.hasNext()){
                SysRole next = iterator.next();
                if(next.getCode().equals("ADMIN")){
                    isAdmin=true;
                    break;
                }
            }
        }

        if(isAdmin) {
            params.remove("userId");
        }else {
            SysUser user = sysUserDao.findById(userId);
            Long companyId = user.getCompanyId();
            params.remove("userId");
            params.put("companyId",companyId);
        }
        int total =interResumeDao.count(params);
        List<InterResume> list= Collections.emptyList();
        try{
            if (total>0){
                PageUtil.pageParamConver(params,true);
                list=interResumeDao.findList(params);
            }
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return PageResult.<InterResume>builder().data(list).code(200).count((long) total).build();
    }

    @Override
    public List<ResumeDto> findResumesByAdresser(Map<String,Object> params) {
        PageUtil.pageParamConver(params,true);
        return interResumeDao.findResumesByAdresser(params);
    }

    @Override
    public List<ResumeDto> findResumesByRecipitien(Map<String,Object> params) {
        return interResumeDao.findResumesByRecipitien(params);
    }

}
